package com.baizhi.service.impl;

import com.baizhi.dao.FavoriteDao;
import com.baizhi.entity.Favorite;
import com.baizhi.feign.VideoFeign;
import com.baizhi.po.FeedVideoPO;
import com.baizhi.po.PlayedPO;
import com.baizhi.service.FavoriteService;
import com.baizhi.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏(Favorite)表服务实现类
 *
 * @author makejava
 * @since 2021-09-28 11:00:10
 */
@Service("favoriteService")
@Transactional
public class FavoriteServiceImpl extends ServiceImpl<FavoriteDao, Favorite> implements FavoriteService {
    private static final Logger log1 = LoggerFactory.getLogger(FavoriteServiceImpl.class);
    @Autowired
    private FavoriteDao favoriteDao;
    @Autowired
    private VideoFeign videoFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;


    /**
     * 分页查询该用户的收藏
     *
     * @param uid
     * @param page
     * @param per_page
     */
    @Override
    public List<PlayedPO> queryAll(Integer uid, Integer page, Integer per_page) {
        //创建分页对象
        Page<Favorite> page1 = new Page<>(page, per_page);
        //创建条件对象
        LambdaQueryWrapper<Favorite> queryWrapper = new LambdaQueryWrapper<>();
        //条件列表
        queryWrapper.orderByDesc(Favorite::getUpdatedAt);
        queryWrapper.eq(Favorite::getUid, uid);
        //获得分页数据
        Page<Favorite> favoritePage = favoriteDao.selectPage(page1, queryWrapper);
        //获得数据
        List<Favorite> records = favoritePage.getRecords();
        /**
         * 根据数据遍历，根据对象获取对应的数据
         */
        //因为返回数据与播放历史返回数据一致，所以使用PlayedPO【复用】
        //创建返回数据集合
        List<PlayedPO> arrayList = new ArrayList<>();
        records.forEach(favorite -> {
            //创建返回对象
            PlayedPO playedPO = new PlayedPO();

            //条件数据
            Integer userId = favorite.getUid();
            Integer videoId = favorite.getVideoId();

            //处理空置数据
            //1.根据视频id获取视频对应的数据
            FeedVideoPO feedVideoPO = videoFeign.queryByVideosId(videoId);
            //将数据cp至返回对象中
            BeanUtils.copyProperties(feedVideoPO, playedPO);
            //点赞量
            HashOperations hashOperations = redisTemplate.opsForHash();
            Long size = hashOperations.size("video_like_id:" + videoId);
            log1.info("video_like_id中有多少key {}", size);
            Integer likes = size.intValue();
            playedPO.setLikes(likes);

            //	up主
            String upName = userService.queryByUid(userId);
            playedPO.setUploader(upName);
            //将数据保存到集合中
            arrayList.add(playedPO);

        });
        return arrayList;
    }
}

